package com.mud.mudcommune.view.fragment.twoLevel;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mud.mudcommune.R;
import com.mud.mudcommune.adapter.SubjectAdapter;
import com.mud.mudcommune.entry.SubjectEntry;
import com.mud.mudcommune.http.ApiService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SubjectFragment extends Fragment implements Callback<SubjectEntry> {
    @BindView(R.id.Subject_ListView)
    ListView listView;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.case_back)
    ImageView back;
    @BindView(R.id.loading_layout)
    LinearLayout loadingLayout;
    private SubjectAdapter Adapter;
    public static Fragment fragment = new SubjectFragment();

    public static Fragment getInstance() {
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_subject, container, false);
        ButterKnife.bind(this, view);
        title.setText("专题活动");
        requestData();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.case_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.case_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    private void requestData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Call<SubjectEntry> call = apiService.getSubject();
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<SubjectEntry> call, Response<SubjectEntry> response) {
        loadingLayout.setVisibility(View.GONE);
        SubjectEntry body = response.body();
        if (body.getStatus() == 200) {
            List<SubjectEntry.Subject> data = body.getData();
            Adapter = new SubjectAdapter(getActivity(), data, fragment, getFragmentManager());
            listView.setAdapter(Adapter);
        }
    }

    @Override
    public void onFailure(Call<SubjectEntry> call, Throwable t) {
        Toast.makeText(getActivity(), "数据请求失败", Toast.LENGTH_SHORT).show();
    }
}
